﻿#if UNITY_EDITOR

using System; 
using UnityEditor;
using UnityEngine;
using XFABManager;

namespace XFGameFramework.SkillSystem
{
    public class CustomSkillConfigDataIconGUI : CustomPropertyGUI
    {


        private GUIContent iconContent;
        private GUIContent dropDownContent;
        private GUIContent dropDownItemContent;
        private bool iconFoldout = true; 

        public override void OnPropertyGUI(SerializedProperty serializedProperty)
        {
            SerializedProperty projectName = serializedProperty.FindPropertyRelative("projectName");
            SerializedProperty iconName = serializedProperty.FindPropertyRelative("iconName");

            if (iconContent == null)
                iconContent = new GUIContent("Icon", "技能图标");

            if (dropDownContent == null)
                dropDownContent = new GUIContent();

            if (dropDownItemContent == null)
                dropDownItemContent = new GUIContent();

            EditorGUILayout.LabelField(string.Empty, "技能图标", "BoldLabel");

            iconFoldout = EditorGUILayout.Foldout(iconFoldout, iconContent);

            if (iconFoldout)
            {
                GUILayout.BeginHorizontal();

                GUILayout.Space(13);

                GUILayout.BeginVertical();

                XFABProject project = XFABProjectManager.Instance.GetProject(projectName.stringValue);

                if (!string.IsNullOrEmpty(projectName.stringValue))
                {
                    project = XFABProjectManager.Instance.GetProject(projectName.stringValue);
                    if (project == null)
                    {
                        projectName.stringValue = string.Empty;
                        projectName.serializedObject.ApplyModifiedProperties();
                    }
                }

                dropDownContent.text = string.IsNullOrEmpty(projectName.stringValue) ? "无" : projectName.stringValue;

                GUILayout.BeginHorizontal();
                GUILayout.Label("projectName", GUILayout.ExpandWidth(false));
                GUILayout.Space(70);
                if (EditorGUILayout.DropdownButton(dropDownContent, FocusType.Passive))
                {
                    GenericMenu menu = new GenericMenu();

                    int count = XFABProjectManager.Instance.Projects.Count;

                    if (count != 0)
                    {
                        foreach (var item in XFABProjectManager.Instance.Projects)
                        {
                            GUIContent content = new GUIContent();
                            content.text = item.name;
                            menu.AddItem(content, projectName.stringValue == item.name, () =>
                            { 
                                projectName.stringValue = item.name;
                                projectName.serializedObject.ApplyModifiedProperties();
                            });
                        }
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("无"));

                        menu.AddSeparator(string.Empty);

                        dropDownItemContent.text = "新建模块";
                        dropDownItemContent.tooltip = "未查询到资源模块,请创建后重试!";
                        menu.AddItem(dropDownItemContent, false, () =>
                        {
                            Type type = AssemblyTools.GetType("XFABManager.XFAssetBundleManagerProjects");
                            EditorWindow window = EditorWindow.GetWindow(type, false, "XFABManager");
                            window.Show();
                        });
                    }

                    menu.ShowAsContext();
                }

                GUILayout.EndHorizontal();


                EditorGUI.BeginChangeCheck();

                iconName.stringValue = EditorGUILayout.TextField("iconName", iconName.stringValue);

                if (EditorGUI.EndChangeCheck())
                { 
                    iconName.serializedObject.ApplyModifiedProperties();
                }

                GUILayout.EndVertical();

                GUIContent icon_content = EditorAssetTool.GetTextureGUIContent(projectName.stringValue, iconName.stringValue);

                if (GUILayout.Button(icon_content, GUILayout.Width(39), GUILayout.Height(39)))
                { 
                    EditorGUIUtility.PingObject(icon_content.image); 
                }


                GUILayout.EndHorizontal();
            }
        } 
    }

}

#endif
